package cn.lyjuan.dwz_manage.core.java.action;


import cn.lyjuan.dwz_manage.core.base.pojo.ActionPojo;
import cn.lyjuan.dwz_manage.core.base.pojo.ManagePojo;
import cn.lyjuan.dwz_manage.core.base.pojo.RolePojo;
import cn.lyjuan.dwz_manage.core.java.dto.GroupActionBean;
import cn.lyjuan.dwz_manage.core.java.dto.QueryResultBean;
import cn.lyjuan.dwz_manage.core.java.service.ActionService;
import cn.lyjuan.dwz_manage.core.java.service.ManageService;
import cn.lyjuan.dwz_manage.core.java.service.RoleService;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 * Created by ly on 2015/3/6.
 */
@Controller
@RequestMapping("role/")
public class RoleAction extends BaseAction
{
    @Autowired
    @Qualifier(value = "mgRoleService")
    private RoleService roleSer;

    @Autowired
    @Qualifier("mgManageService")
    private ManageService manageSer;

    @Autowired
    @Qualifier("mgActionService")
    private ActionService actionSer;

    @RequestMapping("roleList")
    public void roleList(Model model)
    {
        Map<String, String> param = this.paramsToMap();

        ManagePojo manage = manageSer.queryManageByName(this.getUserName());

        QueryResultBean<RolePojo> bean = roleSer.queryRolesForManage(manage, param, this.getDwzPn(), this.getDwzPs());

        this.pkgManagePage(bean.getTotalCount());
        model.addAttribute("roles", bean.getList());
    }

    /**
     * 添加角色
     * @param role
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping("addRole")
    @ResponseBody
    public void addRole(RolePojo role, HttpServletRequest req, HttpServletResponse resp)
    {
        ManagePojo manager = manageSer.queryManageByName(this.getUserName());

        role = roleSer.addRole(manager, role);

        this.returnDwzAjaxSucc();
    }

    /**
     * 请求更新角色信息页面
     * @param roleid
     * @param req
     * @return
     */
    @RequestMapping("preUpdateRole")
    public void preUpdateRole(int roleid, HttpServletRequest req) throws NotFoundException
    {
        ManagePojo manager = manageSer.queryManageByName(this.getUserName());

        RolePojo role = roleSer.queryRoleForManage(roleid);

        if (null == role)
            throw new NotFoundException("未找到该角色信息");

        req.setAttribute("role", role);
    }

    /**
     * 更新角色信息
     * @param name
     * @return
     */
    @RequestMapping("updateRole")
    @ResponseBody
    public void updateRole(int id, String name) throws NotFoundException
    {
        ManagePojo manager = manageSer.queryManageByName(this.getUserName());

        RolePojo fromRole = roleSer.queryRoleForManage(id);
        RolePojo toRole = new RolePojo();
        toRole.setName(name);
        toRole.setId(id);

        roleSer.saveRole(fromRole, toRole);

        this.returnDwzAjaxSucc();
    }

    @RequestMapping("preRelateAction")
    public void preRelateAction(int roleid, Model model)
    {
        ManagePojo manage = manageSer.queryManageByName(this.getUserName());
        // 被操作的角色
        RolePojo role = roleSer.queryRoleForManage(roleid);
        // 当前角色有权限的请求
        List<GroupActionBean> actions = actionSer.queryActions(manage, false);

        // 当前角色有权限的请求
        List<ActionPojo> hadActions = actionSer.queryActionsForRole(roleid);

        model.addAttribute("role", role);
        model.addAttribute("actions", actions);
        model.addAttribute("hadActions", hadActions);
    }

    /**
     * 角色关联请求
     * @param roleid
     * @param actionIds
     */
    @RequestMapping("relateAction")
    @ResponseBody
    public void relateAction(int roleid, Integer[] actionIds)
    {
        // 当前用户
        ManagePojo manage = manageSer.queryManageByName(this.getUserName());
        // 被操作的角色
        RolePojo role = roleSer.queryRoleForManage(roleid);

        // 当前用户有权限的请求
        List<ActionPojo> hadActions = actionSer.queryActions(manage, null, 1, Integer.MAX_VALUE).getList();

        // 关联角色
        roleSer.relateAction(manage, hadActions, role, actionIds);

        this.returnDwzAjaxSucc();
    }
}
